import { fileURLToPath, URL } from 'node:url';
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import vueDevTools from 'vite-plugin-vue-devtools';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
import { visualizer } from 'rollup-plugin-visualizer';
import viteCompression from 'vite-plugin-compression';
//  ElementPlus的Icon自动导入
import Icons from 'unplugin-icons/vite';
import IconsResolver from 'unplugin-icons/resolver';
import legacy from '@vitejs/plugin-legacy';
import postcssPresetEnv from 'postcss-preset-env';
const polyfills = [
  'es.symbol',
  'es.array.filter',
  'es.promise',
  'es.promise.finally',
  'es.object.assign',
  'es.map',
  'es.set',
  'es.array.for-each',
  'es.object.define-properties',
  'es.object.define-property',
  'es.object.get-own-property-descriptor',
  'es.object.get-own-property-descriptors',
  'es.object.keys',
  'es.object.to-string',
  'web.dom-collections.for-each',
  'esnext.global-this',
  'esnext.string.match-all',
  'es.array.iterator',
  'es.string.includes',
  'es.string.starts-with',
  'es.object.values',
];
// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueJsx(),
    vueDevTools(),
    createSvgIconsPlugin({
      iconDirs: [fileURLToPath(new URL('./src/assets/icons/svg', import.meta.url))],
      symbolId: 'icon-[dir]-[name]',
    }),
    AutoImport({
      resolvers: [
        ElementPlusResolver(),
        IconsResolver({
          prefix: 'Icon',
        }),
      ],
    }),
    Components({
      resolvers: [
        ElementPlusResolver(),
        IconsResolver({
          enabledCollections: ['ep'],
        }),
      ],
    }),
    // 自动安装图标
    Icons({
      autoInstall: true,
    }),
    // 打包结束后会自动打开体积分析页面
    visualizer({ open: true }),
    viteCompression({
      verbose: true, // 是否在控制台中输出压缩结果
      threshold: 10240, // 如果体积大于阈值，将被压缩，单位为b，体积过小时请不要压缩，以免适得其反
      algorithm: 'gzip', // 压缩算法，可选['gzip'，' brotliccompress '，'deflate '，'deflateRaw']
      ext: '.gz',
      deleteOriginFile: false, // 源文件压缩后是否删除
    }),
    legacy({
      targets: ['defaults', 'not IE 11', 'chrome>=49'],
      //针对传统浏览器提供额外的polyfills支持
      polyfills: polyfills,
      //针对现代浏览器提供额外的polyfills支持,这些polyfills不会被打包到传统构建中。
      modernPolyfills: polyfills,
    }),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
    },
  },
  css: {
    preprocessorOptions: {
      less: {
        additionalData: `
          @import "@/assets/styles/variables.less";
          @import "@/assets/styles/mixins.less";
        `,
      },
    },
    postcss: {
      plugins: [
        postcssPresetEnv({
          stage: 1,
          autoprefixer: {
            grid: true,
          },
          browsers: ['last 2 versions', 'not IE 11', 'chrome>=49'],
        }),
      ],
    },
  },
  build: {
    target: 'es2015',
    rollupOptions: {
      output: {
        manualChunks: {
          vue: ['vue', 'vue-router', 'pinia'],
          elementPlus: ['@element-plus/icons-vue', 'element-plus'],
        },
        chunkFileNames: 'static/js/[name]-[hash].js',
        entryFileNames: 'static/js/[name]-[hash].js',
        assetFileNames: 'static/[ext]/[name]-[hash].[ext]',
      },
    },
  },
});
